********************************************************************************
********************************************************************************
** Robustness analysis 12
** Excluding best-performing schools to check elite school sensitivity
********************************************************************************
********************************************************************************

** Load data
use "$dataraw_path\data_ftna_publication.dta", clear


** Excluding private schools until average school fees are similar to average
*	spending per pupil in public schools
preserve
	use "$dataraw_path\nps\2014-2015\HH_SEC_C.dta", clear
	merge m:1 y4_hhid using "$dataraw_path\nps\2014-2015\HH_SEC_A.dta", ///
		keepusing(y4_weight) nogen

	keep if inlist(hh_c09,21,22,23,24,31,32)

	sum hh_c28_1 [aw=y4_weight] if inlist(hh_c12,3,5,7), d
	sum hh_c28_1 [aw=y4_weight] if inlist(hh_c12,3,5,7) & hh_c28_1<1300000, d
restore


** Find 1%, 15%, and median private school performance and remove them from the
*	sample and reestimate the preferred model
bys school_id year: egen temp1 = mean(gpa_ftna_all)

preserve
	duplicates drop school_id year, force
	xtile temp2 = temp1 if year==2015 & private==1, nq(100)
	xtile temp3 = temp1 if year==2016 & private==1, nq(100)
	xtile temp4 = temp1 if year==2017 & private==1, nq(100)
	
	sum temp1 if private==1 & year==2015 & temp2<100
	sum temp1 if private==1 & year==2016 & temp3<100
	sum temp1 if private==1 & year==2017 & temp4<100
	
	sum temp1 if private==1 & year==2015 & temp2<86
	sum temp1 if private==1 & year==2016 & temp3<86
	sum temp1 if private==1 & year==2017 & temp4<86
	
	sum temp1 if private==1 & year==2015 & temp2<51
	sum temp1 if private==1 & year==2016 & temp3<51
	sum temp1 if private==1 & year==2017 & temp4<51
restore

gen temp2 = 1 if private==1 & (temp1>3.9012 & year==2015 | ///
	temp1>3.8904 & year==2016 | temp1>3.8769 & year==2017)
gen temp3 = 1 if private==1 & (temp1>3.2112 & year==2015 | ///
	temp1>2.9585 & year==2016 | temp1>3.0667 & year==2017)
gen temp4 = 1 if private==1 & (temp1>2.2607 & year==2015 | ///
	temp1>1.9925 & year==2016 | temp1>2.1317 & year==2017)	

eststo rob12_1: areg gpa_ftna_core_sd private female uncommon_name ///
	peers_score_core_sd peers_fail_share peers_as_share gpa_psle_other_sd ///
	if sample==1 & temp2!=1, cl(school_id) a(group_id)

eststo rob12_2: areg gpa_ftna_core_sd private female uncommon_name ///
	peers_score_core_sd peers_fail_share peers_as_share gpa_psle_other_sd ///
	if sample==1 & temp3!=1, cl(school_id) a(group_id)

eststo rob12_3: areg gpa_ftna_core_sd private female uncommon_name ///
	peers_score_core_sd peers_fail_share peers_as_share gpa_psle_other_sd ///
	if sample==1 & temp4!=1, cl(school_id) a(group_id)

drop temp1-temp4


** Find students progressing to best public schools and their primary school
*	peers
bys school_id: egen temp_school_performance = mean(gpa_ftna_all)
bys school_id: gen temp_school_count = _n
bys school_id: gen temp_school_count_max = _N
replace temp_school_performance = . if temp_school_count>1 | private==1 | ///
	temp_school_count_max<25
xtile temp_best_public1 = temp_school_performance, nq(100)
gen temp_best_public2 = (temp_best_public1==100)
bys school_id: egen temp_best_public3 = max(temp_best_public2)
bys group_id: egen temp_best_public4 = max(temp_best_public3)
gen temp_best_public5 = (temp_best_public3==1 | temp_best_public4==1 & ///
	private==1)

eststo rob12_4: areg gpa_ftna_core_sd private female ///
		uncommon_name peers_score_core_sd peers_fail_share peers_as_share ///
		gpa_psle_other_sd ///
		if sample==1 & temp_best_public5==1, cl(school_id) a(group_id)

drop temp_school_performance-temp_best_public5


** Output
esttab rob12_* using "$out_path\tableb9.tex", replace se ///
stats(N r2, fmt(%12.3gc) labels("\(N\)" "\(R^2\)")) compress nomtitles ///
starlevels("" 0.01) substitute(\_ _) b(3) ///
/*KEEP*/k(private female uncommon_name peers_score_core_sd peers_fail_share ///
peers_as_share gpa_psle_other_sd) ///
/*ORDER*/o(private female uncommon_name peers_score_core_sd peers_fail_share ///
peers_as_share gpa_psle_other_sd) ///
/*LABELS*/varl(private "\$Private_s$" female "\$Female$" ///
uncommon_name "\$\textit{Uncommon name}$" ///
peers_score_core_sd "\$\textit{Peers PSLE}_{s}$" ///
peers_fail_share "\$\textit{Peers failed}_{s}$" ///
peers_as_share "\$\textit{Peers with A}_{s}$" ///
gpa_psle_other_sd "\$\textit{GPA other}_{p} \textit{ (PSLE)}$")
